﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using YArchitech.Revit.RevitExtension;

namespace HYCableTrayConnection
{
	internal class HYTeeConnection
	{
		public HYTeeConnection(Document m_doc, HYCableTray<CableTray> ct1, HYCableTray<CableTray> ct2, HYCableTray<CableTray> ct3, VerticalCableTrayFittingTypeGroup vcf, XYZ intersectionPoint)
		{
			this.doc = m_doc;
			this.myCustomCableTray = ct1;
			this.myCustomCableTray1 = ct2;
			this.myCustomCableTray3 = ct3;
			this.vcf = vcf;
			this.intersectionPoint = intersectionPoint;
			this.GetCableTrayConnectors(intersectionPoint);
		}

		private void GetCableTrayConnectors(XYZ intersectionPoint)
		{
			this.connector = this.myCustomCableTray.GetCloseConnector(intersectionPoint);
			this.connector1 = this.myCustomCableTray1.GetCloseConnector(intersectionPoint);
			this.connector2 = this.myCustomCableTray3.GetCloseConnector(intersectionPoint);
			if (this.connector == null || this.connector1 == null || this.connector2 == null || this.connector.IsConnected || this.connector1.IsConnected || this.connector2.IsConnected)
			{
				throw new MyException("桥架已经连接");
			}
			this.otherVerticalConnector = Common.GetOtherConnector(this.myCustomCableTray, this.connector);
			this.otherConnector1 = Common.GetOtherConnector(this.myCustomCableTray1, this.connector1);
			this.otherConnector2 = Common.GetOtherConnector(this.myCustomCableTray3, this.connector2);
		}

		public bool NewVerticalTeeFitting()
		{
			this.verticalCableTrayFittingType = this.GetVerticalTeeFittingType();
			FamilySymbol fittingType = CableTrayFitingSel.GetVerticalCableTrayFittingType(this.doc, this.verticalCableTrayFittingType, this.myCustomCableTray);
			this.fitting = this.NewTeeFitting(this.verticalCableTrayFittingType, fittingType);
			this.RotateVerticalTeeFitting();
			HYCableTray<CableTray>[] trayWidthSort = Common.GetTrayWidthSort(new HYCableTray<CableTray>[]
			{
				this.myCustomCableTray1,
				this.myCustomCableTray3
			});
			HYCableTray<CableTray> hycableTray = trayWidthSort[0];
			HYCableTray<CableTray> hycableTray2 = trayWidthSort[1];
			double num = 0.0;
			double width = hycableTray2.CableTray.Width;
			double height = hycableTray2.CableTray.Height;
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(hycableTray.CableTray.Width);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(hycableTray.CableTray.Height);
			if ((this.verticalCableTrayFittingType == VerticalCableTrayFittingType.VerticalDownTee || this.verticalCableTrayFittingType == VerticalCableTrayFittingType.VerticalUpTee) && this.myCustomCableTray.CableTray.Width != hycableTray.CableTray.Width)
			{
				num = this.myCustomCableTray.CableTray.Width;
				this.myCustomCableTray.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(hycableTray.CableTray.Width);
			}
			this.doc.Regenerate();
			this.SetFittingParams(this.verticalCableTrayFittingType, hycableTray);
			ElementId elementId = this.myCustomCableTray3.CableTray.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId();
			double num2 = (this.doc.GetElementById(elementId) as Level).GetParameter(BuiltInParameter.LEVEL_ELEV).AsDouble();
			double num3 = this.myCustomCableTray3.CableTray.get_Parameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble() + num2;
			this.fitting.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).Set(num3);
			this.TranslationTeeFitting(hycableTray.CableTray.Width);
			XYZ xyz = this.myCustomCableTray.IntersectionPoint(this.myCustomCableTray1.CableTray);
			if (xyz == null)
			{
				xyz = this.myCustomCableTray3.GetLineProject(this.myCustomCableTray.EndXYZ);
			}
			this.connector = this.myCustomCableTray.GetCloseConnector(xyz);
			this.TeeFittingConnect();
			Common.RefreshModel(this.doc, this.fitting);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(width);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(height);
			this.myCustomCableTray.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(num);
			this.doc.Regenerate();
			return true;
		}

		public bool NewCornerVerticalTeeFitting()
		{
			HYCableTray<CableTray> hycableTray = null;
			HYCableTray<CableTray> hycableTray2 = null;
			Connector conn = null;
			Connector connector = null;
			if (this.vcf == VerticalCableTrayFittingTypeGroup.VerticalCornerTee)
			{
				if (this.connector.CoordinateSystem.BasisY.IsAlmostEqualTo(this.connector1.CoordinateSystem.BasisZ, 0.01))
				{
					hycableTray = this.myCustomCableTray1;
					hycableTray2 = this.myCustomCableTray3;
					conn = this.connector1;
					connector = this.connector2;
				}
				else
				{
					if (!this.connector.CoordinateSystem.BasisY.IsAlmostEqualTo(this.connector2.CoordinateSystem.BasisZ, 0.01))
					{
						throw new MyException("请检查桥架位置");
					}
					hycableTray = this.myCustomCableTray3;
					hycableTray2 = this.myCustomCableTray1;
					conn = this.connector2;
					connector = this.connector1;
				}
			}
			this.verticalCableTrayFittingType = this.GetTeeFittingType(conn, connector);
			FamilySymbol fittingType = CableTrayFitingSel.GetVerticalCableTrayFittingType(this.doc, this.verticalCableTrayFittingType, this.myCustomCableTray);
			this.fitting = this.NewTeeFitting(this.verticalCableTrayFittingType, fittingType);
			this.RotateCornerVerticalTeeFitting(conn);
			double width = this.myCustomCableTray.CableTray.Width;
			double height = hycableTray2.CableTray.Height;
			this.myCustomCableTray.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(hycableTray.CableTray.Width);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(hycableTray.CableTray.Height);
			this.doc.Regenerate();
			this.SetFittingParams1(this.verticalCableTrayFittingType, hycableTray, hycableTray2);
			this.TranslationTeeFitting(hycableTray.CableTray.Width);
			XYZ xyz = this.myCustomCableTray.IntersectionPoint(this.myCustomCableTray1.CableTray);
			if (xyz == null)
			{
				xyz = this.myCustomCableTray3.GetLineProject(this.myCustomCableTray.EndXYZ);
			}
			this.connector = this.myCustomCableTray.GetCloseConnector(xyz);
			this.TeeFittingConnect();
			this.doc.Regenerate();
			Common.RefreshModel(this.doc, this.fitting);
			this.myCustomCableTray.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(width);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(height);
			this.doc.Regenerate();
			return true;
		}

		public bool NewVerticalTeeFitting1()
		{
			this.verticalCableTrayFittingType = this.JudgeTeeFittingType();
			FamilySymbol fittingType = CableTrayFitingSel.GetVerticalCableTrayFittingType(this.doc, this.verticalCableTrayFittingType, this.myCustomCableTray);
			this.fitting = this.NewTeeFitting(this.verticalCableTrayFittingType, fittingType);
			this.RotateFitting3(this.verticalCableTrayFittingType);
			HYCableTray<CableTray>[] trayWidthSort = Common.GetTrayWidthSort(new HYCableTray<CableTray>[]
			{
				this.myCustomCableTray1,
				this.myCustomCableTray3
			});
			HYCableTray<CableTray> hycableTray = trayWidthSort[0];
			HYCableTray<CableTray> hycableTray2 = trayWidthSort[1];
			double num = 0.0;
			double width = hycableTray2.CableTray.Width;
			double height = hycableTray2.CableTray.Height;
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(hycableTray.CableTray.Width);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(hycableTray.CableTray.Height);
			if ((this.verticalCableTrayFittingType == VerticalCableTrayFittingType.VerticalDownTee || this.verticalCableTrayFittingType == VerticalCableTrayFittingType.VerticalUpTee) && this.myCustomCableTray.CableTray.Width != hycableTray.CableTray.Width)
			{
				num = this.myCustomCableTray.CableTray.Width;
				this.myCustomCableTray.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(hycableTray.CableTray.Width);
			}
			this.doc.Regenerate();
			this.SetFittingParams(this.verticalCableTrayFittingType, hycableTray);
			ElementId elementId = this.myCustomCableTray.CableTray.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId();
			double num2 = (this.doc.GetElementById(elementId) as Level).GetParameter(BuiltInParameter.LEVEL_ELEV).AsDouble();
			double num3 = this.myCustomCableTray.CableTray.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble() + num2;
			this.fitting.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).Set(num3);
			this.doc.Regenerate();
			XYZ xyz = this.myCustomCableTray.IntersectionPoint(this.myCustomCableTray1.CableTray);
			if (xyz == null)
			{
				xyz = this.myCustomCableTray3.GetLineProject(this.myCustomCableTray.EndXYZ);
			}
			this.connector = this.myCustomCableTray.GetCloseConnector(xyz);
			this.TeeFittingConnect2();
			Common.RefreshModel(this.doc, this.fitting);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(width);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(height);
			this.myCustomCableTray.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(num);
			this.doc.Regenerate();
			return true;
		}

		public bool NewDirectionVerticalTeeFitting()
		{
			this.verticalCableTrayFittingType = this.JudgeTeeFittingType2();
			FamilySymbol fittingType = CableTrayFitingSel.GetVerticalCableTrayFittingType(this.doc, this.verticalCableTrayFittingType, this.myCustomCableTray);
			this.fitting = this.NewTeeFitting(this.verticalCableTrayFittingType, fittingType);
			this.RotateFitting4();
			HYCableTray<CableTray>[] trayWidthSort = Common.GetTrayWidthSort(new HYCableTray<CableTray>[]
			{
				this.myCustomCableTray1,
				this.myCustomCableTray3
			});
			HYCableTray<CableTray> hycableTray = trayWidthSort[0];
			HYCableTray<CableTray> hycableTray2 = trayWidthSort[1];
			double width = hycableTray2.CableTray.Width;
			double height = hycableTray2.CableTray.Height;
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(hycableTray.CableTray.Width);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(hycableTray.CableTray.Height);
			this.doc.Regenerate();
			this.SetFittingParams(this.verticalCableTrayFittingType, hycableTray);
			ElementId elementId = this.myCustomCableTray.CableTray.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId();
			double num = (this.doc.GetElementById(elementId) as Level).GetParameter(BuiltInParameter.LEVEL_ELEV).AsDouble();
			double num2 = this.myCustomCableTray.CableTray.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble() + num;
			this.fitting.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).Set(num2);
			this.TranslationTeeFitting(this.connector1, this.connector2, this.fitting, this.myCustomCableTray.CableTray.Width);
			XYZ lineProject = this.myCustomCableTray.GetLineProject(this.myCustomCableTray1.EndXYZ);
			this.connector2 = this.myCustomCableTray3.GetCloseConnector(lineProject);
			this.connector1 = this.myCustomCableTray1.GetCloseConnector(lineProject);
			this.TeeFittingConnect2();
			Common.RefreshModel(this.doc, this.fitting);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_WIDTH_PARAM).Set(width);
			hycableTray2.CableTray.GetParameter(BuiltInParameter.RBS_CABLETRAY_HEIGHT_PARAM).Set(height);
			this.doc.Regenerate();
			return true;
		}

		private VerticalCableTrayFittingType GetVerticalTeeFittingType()
		{
			bool isUp = this.otherVerticalConnector.Origin.Z > this.connector1.Origin.Z;
			return this.JudgeVerticalTeeFittingType(isUp);
		}

		private VerticalCableTrayFittingType JudgeVerticalTeeFittingType(bool isUp)
		{
			VerticalCableTrayFittingType result = VerticalCableTrayFittingType.Undefined;
			if (this.vcf == VerticalCableTrayFittingTypeGroup.VerticalTee)
			{
				if (isUp)
				{
					result = VerticalCableTrayFittingType.VerticalDownTee;
				}
				else
				{
					result = VerticalCableTrayFittingType.VerticalUpTee;
				}
			}
			else if (VerticalCableTrayFittingTypeGroup.VerticalSideTee == this.vcf)
			{
				if (isUp)
				{
					result = VerticalCableTrayFittingType.VerticalDownSideTee;
				}
				else
				{
					result = VerticalCableTrayFittingType.VerticalUpSideTee;
				}
			}
			return result;
		}

		private void RotateVerticalTeeFitting()
		{
			Transform coordinateSystem = this.connector.CoordinateSystem;
			Connector connector = null;
			foreach (Connector connector2 in RevitUtils.GetConduitFittingConnectors(this.fitting))
			{
				if (connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0), 0.01) || connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0), 0.01))
				{
					connector = connector2;
					break;
				}
			}
			double num = connector.CoordinateSystem.BasisY.AngleOnPlaneTo(-coordinateSystem.BasisY, XYZ.BasisZ);
			if (num != 0.0)
			{
				Line line = Line.CreateUnbound((this.fitting.Location as LocationPoint).Point, XYZ.BasisZ);
				this.fitting.Location.Rotate(line, num);
			}
		}

		private void TranslationTeeFitting(double range)
		{
			foreach (Connector connector in RevitUtils.GetConduitFittingConnectors(this.fitting))
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(XYZ.BasisZ, 0.01) || connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-XYZ.BasisZ, 0.01))
				{
					XYZ xyz = new XYZ(connector.Origin.X, connector.Origin.Y, this.connector.Origin.Z) - this.connector.Origin;
					double length = xyz.GetLength();
					if (length <= 1E-05)
					{
						break;
					}
					if (length > range)
					{
						throw new MyException("请检查桥架位置");
					}
					if (!this.otherVerticalConnector.IsConnected)
					{
						this.myCustomCableTray.CableTray.Location.Move(xyz);
						this.myCustomCableTray = new HYCableTray<CableTray>(this.myCustomCableTray.CableTray);
						break;
					}
					throw new MyException("请检查桥架位置");
				}
			}
		}

		private void TeeFittingConnect()
		{
			XYZ lineDirection = this.GetLineDirection(this.doc, this.myCustomCableTray1, this.connector1);
			XYZ lineDirection2 = this.GetLineDirection(this.doc, this.myCustomCableTray3, this.connector2);
			foreach (Connector connector in RevitUtils.GetConduitFittingConnectors(this.fitting))
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(lineDirection, 0.01))
				{
					this.connector1.ConnectTo(connector);
				}
				else if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(lineDirection2, 0.01))
				{
					this.connector2.ConnectTo(connector);
				}
				else
				{
					this.connector.ConnectTo(connector);
				}
			}
		}

		private VerticalCableTrayFittingType GetTeeFittingType(Connector connector1, Connector connector2)
		{
			bool isUp = this.otherVerticalConnector.Origin.Z > connector1.Origin.Z;
			return this.JudgeTeeFittingType(connector1, connector2, isUp);
		}

		private VerticalCableTrayFittingType JudgeTeeFittingType()
		{
			VerticalCableTrayFittingType result = VerticalCableTrayFittingType.Undefined;
			XYZ xyz = (this.otherVerticalConnector.Origin - this.connector.Origin).Normalize();
			if (xyz.IsAlmostEqualTo(-this.connector1.CoordinateSystem.BasisY, 0.01))
			{
				result = VerticalCableTrayFittingType.VerticalDownTee;
			}
			else if (xyz.IsAlmostEqualTo(this.connector1.CoordinateSystem.BasisY, 0.01))
			{
				result = VerticalCableTrayFittingType.VerticalUpTee;
			}
			return result;
		}

		private void RotateFitting3(VerticalCableTrayFittingType fittingType)
		{
			XYZ point = (this.fitting.Location as LocationPoint).Point;
			List<Connector> conduitFittingConnectors = RevitUtils.GetConduitFittingConnectors(this.fitting);
			Connector connector = null;
			foreach (Connector connector2 in conduitFittingConnectors)
			{
				if (connector2.CoordinateSystem.BasisZ.Normalize().IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0), 0.01) || connector2.CoordinateSystem.BasisZ.Normalize().IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0), 0.01))
				{
					connector = connector2;
					break;
				}
			}
			double num = 0.0;
			if (fittingType == VerticalCableTrayFittingType.VerticalDownTee || fittingType == VerticalCableTrayFittingType.VerticalUpTee)
			{
				num = -this.connector1.CoordinateSystem.BasisY.AngleOnPlaneTo(connector.CoordinateSystem.BasisY, XYZ.BasisZ);
			}
			if (num != 0.0)
			{
				Line line = Line.CreateUnbound(point, XYZ.BasisZ);
				this.fitting.Location.Rotate(line, num);
			}
			this.doc.Regenerate();
			foreach (Connector connector3 in RevitUtils.GetConduitFittingConnectors(this.fitting))
			{
				if (connector3.CoordinateSystem.BasisZ.Normalize().IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0), 0.01) || connector3.CoordinateSystem.BasisZ.Normalize().IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0), 0.01))
				{
					connector = connector3;
				}
			}
			Line line2 = Line.CreateUnbound(point, connector.CoordinateSystem.BasisX);
			if (fittingType == VerticalCableTrayFittingType.VerticalDownTee)
			{
				ElementTransformUtils.RotateElement(this.doc, this.fitting.Id, line2, Math.PI*.5);
				return;
			}
			if (fittingType == VerticalCableTrayFittingType.VerticalUpTee)
			{
				ElementTransformUtils.RotateElement(this.doc, this.fitting.Id, line2, -Math.PI*.5);
			}
		}

		private void RotateFitting4()
		{
			Connector connector = null;
			foreach (Connector connector2 in RevitUtils.GetConduitFittingConnectors(this.fitting))
			{
				if (connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0), 0.01))
				{
					connector = connector2;
					break;
				}
			}
			XYZ basisY = connector.CoordinateSystem.BasisY;
			XYZ xyz = -this.connector1.CoordinateSystem.BasisY;
			double num = basisY.AngleOnPlaneTo(xyz, XYZ.BasisZ);
			if (num != 0.0)
			{
				Line line = Line.CreateUnbound((this.fitting.Location as LocationPoint).Point, XYZ.BasisZ);
				this.fitting.Location.Rotate(line, num);
			}
		}

		private VerticalCableTrayFittingType JudgeTeeFittingType2()
		{
			VerticalCableTrayFittingType result = VerticalCableTrayFittingType.Undefined;
			XYZ xyz = (this.otherVerticalConnector.Origin - this.connector.Origin).Normalize();
			XYZ xyz2 = -this.connector1.CoordinateSystem.BasisY;
			double num = xyz.AngleOnPlaneTo(xyz2, XYZ.BasisZ);
			if (Math.Abs(Math.PI*.5 - num) < 0.001)
			{
				result = VerticalCableTrayFittingType.VerticalRightTee;
			}
			else if (Math.Abs(4.71238898038469 - num) < 0.001)
			{
				result = VerticalCableTrayFittingType.VerticalLeftTee;
			}
			return result;
		}

		private void TranslationTeeFitting(Connector otherConnector1, Connector otherConnector3, FamilyInstance fitting, double range)
		{
			Connector connector = null;
			foreach (Connector connector2 in RevitUtils.GetConduitFittingConnectors(fitting))
			{
				if (connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0), 0.01) || connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0), 0.01))
				{
					connector = connector2;
					break;
				}
			}
			if (!otherConnector1.IsConnected)
			{
				XYZ xyz = new XYZ(connector.Origin.X, connector.Origin.Y, otherConnector1.Origin.Z) - otherConnector1.Origin;
				double length = xyz.GetLength();
				if (length <= 1E-05)
				{
					return;
				}
				if (length > range)
				{
					throw new MyException("请检查桥架位置");
				}
				this.myCustomCableTray1.CableTray.Location.Move(xyz);
				this.myCustomCableTray1 = new HYCableTray<CableTray>(this.myCustomCableTray1.CableTray);
			}
			if (otherConnector3.IsConnected)
			{
				return;
			}
			XYZ xyz2 = new XYZ(connector.Origin.X, connector.Origin.Y, otherConnector3.Origin.Z) - otherConnector3.Origin;
			double length2 = xyz2.GetLength();
			if (length2 <= 1E-05)
			{
				return;
			}
			if (length2 <= range)
			{
				this.myCustomCableTray3.CableTray.Location.Move(xyz2);
				this.myCustomCableTray3 = new HYCableTray<CableTray>(this.myCustomCableTray3.CableTray);
				return;
			}
			throw new MyException("请检查桥架位置");
		}

		private void TeeFittingConnect2()
		{
			List<Connector> conduitFittingConnectors = RevitUtils.GetConduitFittingConnectors(this.fitting);
			XYZ xyz = this.GetLineDirection(this.doc, this.myCustomCableTray1, this.connector1).Normalize();
			XYZ xyz2 = this.GetLineDirection(this.doc, this.myCustomCableTray3, this.connector2).Normalize();
			foreach (Connector connector in conduitFittingConnectors)
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(xyz, 0.01))
				{
					this.connector1.ConnectTo(connector);
				}
				else if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(xyz2, 0.01))
				{
					this.connector2.ConnectTo(connector);
				}
				else
				{
					this.connector.ConnectTo(connector);
				}
			}
		}

		private XYZ GetLineDirection(Document doc, HYCableTray<CableTray> hyct, Connector connector)
		{
			XYZ origin = Common.GetOtherConnector(hyct, connector).Origin;
			return YJKLineEx.YJKGetBound(connector.Origin, origin).Direction.Normalize();
		}

		private void SetFittingParams(VerticalCableTrayFittingType type, HYCableTray<CableTray> fittingTray)
		{
			if (type == VerticalCableTrayFittingType.VerticalDownTee || type == VerticalCableTrayFittingType.VerticalUpTee)
			{
				Dictionary<Connector, CableTrayFittingSizeParamer> fittingRelevantParamer = FittingConnectorRelevantParamer.GetFittingRelevantParamer(type, this.fitting);
				fittingRelevantParamer.First<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Width = fittingTray.CableTray.Width;
				fittingRelevantParamer.First<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Height = fittingTray.CableTray.Height;
				fittingRelevantParamer.Last<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Height = this.myCustomCableTray.CableTray.Height;
			}
			else if (type == VerticalCableTrayFittingType.VerticalDownSideTee || type == VerticalCableTrayFittingType.VerticalUpSideTee)
			{
				Dictionary<Connector, CableTrayFittingSizeParamer> fittingRelevantParamer2 = FittingConnectorRelevantParamer.GetFittingRelevantParamer(type, this.fitting);
				fittingRelevantParamer2.First<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Width = fittingTray.CableTray.Width;
				fittingRelevantParamer2.First<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Height = fittingTray.CableTray.Height;
				double width = this.myCustomCableTray.CableTray.Width;
				double num = this.myCustomCableTray.CableTray.Height;
				if (num >= fittingTray.CableTray.Width)
				{
					num = fittingTray.CableTray.Width - 0.032808398950131233;
				}
				fittingRelevantParamer2.Last<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Width = width;
				fittingRelevantParamer2.Last<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Height = num;
			}
			else if (type == VerticalCableTrayFittingType.VerticalRightTee || type == VerticalCableTrayFittingType.VerticalLeftTee)
			{
				Dictionary<Connector, CableTrayFittingSizeParamer> fittingRelevantParamer3 = FittingConnectorRelevantParamer.GetFittingRelevantParamer(type, this.fitting);
				fittingRelevantParamer3.First<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Width = fittingTray.CableTray.Width;
				fittingRelevantParamer3.First<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Height = fittingTray.CableTray.Height;
				fittingRelevantParamer3.Last<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Width = this.myCustomCableTray.CableTray.Width;
				fittingRelevantParamer3.Last<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Height = this.myCustomCableTray.CableTray.Height;
			}
			this.doc.Regenerate();
		}

		private void SetFittingParams1(VerticalCableTrayFittingType type, HYCableTray<CableTray> symTray, HYCableTray<CableTray> nonSymTray)
		{
			if (type == VerticalCableTrayFittingType.VerticalDownCornerTeeA || type == VerticalCableTrayFittingType.VerticalDownCornerTeeB || type == VerticalCableTrayFittingType.VerticalUpCornerTeeA || type == VerticalCableTrayFittingType.VerticalUpCornerTeeB)
			{
				Dictionary<Connector, CableTrayFittingSizeParamer> fittingRelevantParamer = FittingConnectorRelevantParamer.GetFittingRelevantParamer(type, this.fitting);
				fittingRelevantParamer.First<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Width = symTray.CableTray.Width;
				fittingRelevantParamer.First<KeyValuePair<Connector, CableTrayFittingSizeParamer>>().Key.Height = symTray.CableTray.Height;
				double width = nonSymTray.CableTray.Width;
				double num = this.myCustomCableTray.CableTray.Height;
				if (num >= width)
				{
					num = width - 0.032808398950131233;
				}
				fittingRelevantParamer.Keys.ToList<Connector>()[1].Width = width;
				fittingRelevantParamer.Keys.ToList<Connector>()[2].Height = num;
			}
			ElementId elementId = this.myCustomCableTray3.CableTray.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId();
			double num2 = (this.doc.GetElementById(elementId) as Level).GetParameter(BuiltInParameter.LEVEL_ELEV).AsDouble();
			double num3 = this.myCustomCableTray3.CableTray.get_Parameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble() + num2;
			this.fitting.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).Set(num3);
			this.doc.Regenerate();
		}

		private FamilyInstance NewTeeFitting(VerticalCableTrayFittingType type, FamilySymbol fittingType)
		{
			UnifiedModified.ActivateFamilySymbol(fittingType);
			FamilyInstance result = this.doc.Create.NewFamilyInstance(this.intersectionPoint, fittingType, 0);
			this.doc.Regenerate();
			return result;
		}

		private VerticalCableTrayFittingType JudgeTeeFittingType(Connector conn1, Connector conn2, bool isUp)
		{
			VerticalCableTrayFittingType result = VerticalCableTrayFittingType.Undefined;
			XYZ basisZ = conn1.CoordinateSystem.BasisZ;
			XYZ basisZ2 = conn2.CoordinateSystem.BasisZ;
			double num = basisZ.AngleOnPlaneTo(basisZ2, XYZ.BasisZ);
			if (Math.Abs(Math.PI*.5 - num) < 0.001)
			{
				if (isUp)
				{
					result = VerticalCableTrayFittingType.VerticalDownCornerTeeA;
				}
				else
				{
					result = VerticalCableTrayFittingType.VerticalUpCornerTeeA;
				}
			}
			else if (Math.Abs(4.71238898038469 - num) < 0.001)
			{
				if (isUp)
				{
					result = VerticalCableTrayFittingType.VerticalDownCornerTeeB;
				}
				else
				{
					result = VerticalCableTrayFittingType.VerticalUpCornerTeeB;
				}
			}
			return result;
		}

		private void RotateCornerVerticalTeeFitting(Connector conn2)
		{
			Connector connector = null;
			foreach (Connector connector2 in RevitUtils.GetConduitFittingConnectors(this.fitting))
			{
				if (connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0), 0.01) || connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0), 0.01))
				{
					connector = connector2;
					break;
				}
			}
			XYZ basisY = connector.CoordinateSystem.BasisY;
			XYZ xyz = -conn2.CoordinateSystem.BasisZ;
			double num = basisY.AngleOnPlaneTo(xyz, XYZ.BasisZ);
			if (num != 0.0)
			{
				Line line = Line.CreateUnbound((this.fitting.Location as LocationPoint).Point, XYZ.BasisZ);
				this.fitting.Location.Rotate(line, num);
			}
		}

		private Document doc;

		private HYCableTray<CableTray> myCustomCableTray;

		private HYCableTray<CableTray> myCustomCableTray1;

		private HYCableTray<CableTray> myCustomCableTray3;

		private XYZ intersectionPoint;

		private VerticalCableTrayFittingTypeGroup vcf;

		private VerticalCableTrayFittingType verticalCableTrayFittingType = VerticalCableTrayFittingType.Undefined;

		private Connector connector;

		private Connector connector1;

		private Connector connector2;

		private Connector otherVerticalConnector;

		private Connector otherConnector1;

		private Connector otherConnector2;

		private FamilyInstance fitting;
	}
}
